package com.android.djlmapp.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.listener.kaika.ShouDongListListener;
import com.android.djlmapp.model.kaika.PremCardInfo;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.readcard.dialog.MyUsualyDialog;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * 身份证正反面
 */
public class ShouDongDialog extends Dialog {

    private Activity context;
    private ShouDongListListener mTuiDanListListener;
    private ImageView img_zheng;
    private ImageView img_fan;
    private TextView td_ok;
    private TextView td_cancle;

    private String faceUrl = "";
    private String backUrl = "";
    private MyUsualyDialog dialog;
    private boolean isHuZhao = false;
    private int layout;

    public ShouDongDialog(Context context,int layout, ShouDongListListener mTuiDanListListener) {
        super(context, R.style.QiaoDaoDialogStyle);
        this.context = (Activity) context;
        this.mTuiDanListListener = mTuiDanListListener;
        this.layout = layout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout);
        img_zheng = (ImageView) findViewById(R.id.img_zheng);
        img_fan = (ImageView) findViewById(R.id.img_fan);
        td_ok = (TextView) findViewById(R.id.td_ok);
        td_cancle = (TextView) findViewById(R.id.td_cancle);

        img_zheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTuiDanListListener.onZhengOrFanOrder(0);
            }
        });
        img_fan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                backUrl = "";// 每次点击的时候重置数据
                if (TextUtils.isEmpty(faceUrl)) {
                    ToastUtils.showShortToast(context, "请上传身份证正面照!");
                    img_fan.setEnabled(false);
                } else {
                    img_fan.setEnabled(true);
                    mTuiDanListListener.onZhengOrFanOrder(1);
                }
            }
        });
        td_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 判断照片拍了没
                Log.e("项目异常", backUrl + "------1------" + faceUrl);
                dialog = new MyUsualyDialog(context, R.layout.wait_item);
                dialog.setCanceledOnTouchOutside(true);
                dialog.setCancelable(false);
                if (TextUtils.isEmpty(faceUrl)) {
                    ToastUtils.showShortToast(context, "请上传身份证正面照!");
                } else if (TextUtils.isEmpty(backUrl)) {
                    ToastUtils.showShortToast(context, "请上传身份证背面照!");
                } else {
                    if (isHuZhao) {
                        cancel();
                        mTuiDanListListener.onCancleOrder("", "");
                    } else {
                        dialog.show();
                        getCardInfo();
                    }
                }
            }
        });

        td_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancel();
            }
        });
    }

    private void getCardInfo() {
        MyApplication.getInstance().getHttpTools().getCardInfo(backUrl, faceUrl, new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    Gson gson = new Gson();
                    PremCardInfo premCardInfo = gson.fromJson(response.body(), PremCardInfo.class);
                    if (premCardInfo != null) {
                        if (premCardInfo.getCode() == 200) {
                            cancel();
                            mTuiDanListListener.onCancleOrder(premCardInfo.getInfo(), premCardInfo.getNeed_jbr());
                        } else {
                            ToastUtils.showShortToast(context, premCardInfo.getMsg());
                        }

                    } else {
                        ToastUtils.showShortToast(context, "服务器返回数据为空");
                    }

                } catch (Exception e) {
                    Log.e("项目异常", "------数据解析异常------" + e.toString());
                    ToastUtils.showShortToast(context, "数据解析异常");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                // 网络连接失败要重新上传图片
                ToastUtils.showShortToast(context, "上传失败，网络或服务器错误..");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
    }

    public void setUrl(boolean isHuZhao, int photoType, String compressPath, String url) {
        this.isHuZhao = isHuZhao;
        if (photoType == 0) {
            img_fan.setEnabled(true);
            faceUrl = url;
//            img_zheng.setImageBitmap(myBitmap);
            Glide.with(context).load(compressPath).into(img_zheng);
        } else {
            backUrl = url;
//            img_fan.setImageBitmap(myBitmap);
            Glide.with(context).load(compressPath).into(img_fan);
        }

    }
}
